package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.vo.AttrDeleteReqVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
		//select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
		QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
		String key = (String) params.get("key");
		if(StringUtils.isNotBlank(key)) {
			wrapper.and((obj)->{
				obj.eq("attr_group_id", key).or().like("attr_group_name", key);
			});
		}
		if(categoryId != 0) {
			// 构造查询条件
			wrapper.eq("catelog_id", categoryId);
		}
		IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
		return new PageUtils(page);

	}

}